package com.atguigu.gulimall.authserver.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.constant.AuthServerConstant;
import com.atguigu.common.utils.HttpUtils;
import com.atguigu.common.utils.R;
import com.atguigu.common.vo.MemberRespVo;
import com.atguigu.gulimall.authserver.feign.MemberFeignService;
import com.atguigu.gulimall.authserver.vo.SocialUserVo;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
* @需求描述: 系统管理员-认证服务-社交登录
* @创建人: 郭承乾
* @创建时间: 2020/12/31 13:36
* @修改需求:
* @修改人:
* @修改时间:
* @需求思路:
*/
@Controller
public class OAuth2Controller {

    @Autowired
    MemberFeignService memberFeignService;

    /**
    * @需求描述: 系统管理员 认证服务组 模块 微博社交登录成功回调接口
    * @创建人: 郭承乾
    * @创建时间: 2020/12/31 21:42
    * @修改需求:
    * @修改人:
    * @修改时间:
    * @需求思路:
    */
    @GetMapping("/oauth2.0/weibo/success")
    public String oauthLogin(@RequestParam("code") String code, HttpSession session) throws Exception {
        // https://open.weibo.com/wiki/Oauth2/access_token#_loginLayer_1609422607824 api地址
        // 1、根据code和其他参数换取access_token
        Map<String,String> map = new HashMap<>();
        map.put("client_id","1133714539"); // 申请应用时分配的AppKey。
        map.put("client_secret","f22eb330342e7f8797a7dbe173bd9424"); // 申请应用时分配的AppSecret。
        map.put("grant_type","authorization_code"); // 请求的类型
        map.put("redirect_uri","http://auth.gulimall.com/oauth2.0/weibo/success");
        map.put("code",code);
        // 发送请求
        HttpResponse response = HttpUtils.doPost("https://api.weibo.com/",
                "oauth2/access_token",
                "post",
                new HashMap<>(),
                map,
                new HashMap<>());

        // 状态码为200请求成功
        if (response.getStatusLine().getStatusCode() == 200) {
            // 通过工具类将请求转成json
            String json = EntityUtils.toString(response.getEntity());
            SocialUserVo socialUservVo = JSON.parseObject(json, SocialUserVo.class);
            System.out.println(socialUservVo);
            // 调用gulimall-member注册该社交用户
            R r = memberFeignService.oauthLogin(socialUservVo);
            if (r.getCode() == 0) {
                MemberRespVo data = r.getData("data", new TypeReference<MemberRespVo>() {
                });
                System.out.println(data);
                // 返回的对象放入session
                session.setAttribute(AuthServerConstant.LOGIN_USER,data);
                return "redirect:http://gulimall.com";
            } else {
                // 注册失败
                return "redirect:http://auth.gulimall.com/login.html";
            }
        } else {
            // 请求失败
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }
}